package com.finger.gulimall.product.app;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.finger.common.valid.AddGroup;
import com.finger.common.valid.UpdateGroup;
import com.finger.gulimall.product.entity.ProductAttrValueEntity;
import com.finger.gulimall.product.service.ProductAttrValueService;
import com.finger.gulimall.product.vo.AttrRespVO;
import com.finger.gulimall.product.vo.AttrVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import com.finger.gulimall.product.service.AttrService;
import com.finger.common.utils.PageUtils;
import com.finger.common.utils.R;


/**
 * 商品属性
 *
 * @author finger
 * @email 909128695@qq.com
 * @date 2022-03-16 13:38:29
 */
@RestController
@RequestMapping("product/attr")
public class AttrController {

    @Autowired
    private AttrService attrService;

    @Autowired
    private ProductAttrValueService productAttrValueService;

    /**
     * 列表
     */
    @RequestMapping("/{type}/list/{catId}")
    // @RequiresPermissions("product:attr:list")
    public R list(@RequestParam Map<String, Object> params,
                  @PathVariable Integer catId,
                  @PathVariable String type) {
        PageUtils page = attrService.queryPage(params, catId, type);

        return R.ok().put("page", page);
    }

    /*
     *  请求路径 product/attr/base/listforspu/4
     *
     */
    @GetMapping("/base/listforspu/{spuId}")
    public R listAttrForSpu(@PathVariable Long spuId) {

        // 根据商品spuid 获取该spu下的全部属性值
        List<ProductAttrValueEntity> productAttrValueEntities = productAttrValueService.getListAttrForSpu(spuId);
        return R.ok().put("data", productAttrValueEntities);
    }

    /**
     * /product/attr/update/{spuId}
     * 修改商品规格说明
     */
    @PostMapping("/update/{spuId}")
    public R updateSpuAttrValue(@PathVariable Long spuId,
                                @RequestBody List<ProductAttrValueEntity> entitys) {

        productAttrValueService.updateSpuAttrValues(spuId, entitys);
        return R.ok();
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{attrId}")
    // @RequiresPermissions("product:attr:info")
    public R info(@PathVariable("attrId") Long attrId) {
        AttrRespVO attr = attrService.getDetails(attrId);

        return R.ok().put("attr", attr);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    // @RequiresPermissions("product:attr:save")
    public R save(@Validated(AddGroup.class) @RequestBody AttrVO attr) {
        attrService.saveAttr(attr);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    // @RequiresPermissions("product:attr:update")
    public R update(@Validated(UpdateGroup.class) @RequestBody AttrVO attr) {
        attrService.updateDetails(attr);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    // @RequiresPermissions("product:attr:delete")
    public R delete(@RequestBody Long[] attrIds) {

        // 当删除属性时，级联删除属性和属性分组中间表中的数据
        attrService.removeCascadeDetails(Arrays.asList(attrIds));

        return R.ok();
    }

    @GetMapping("/getAttrName")
    public R getAttrName(@RequestParam("attrId") Long attrId) {
        String attrName = attrService.getAttrNameByAttrId(attrId);

        return R.ok().put("attrName", attrName);
    }
}
